package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.feign.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zhmwi
 * @Date 2021/7/19
 * @Dersciption
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    //查询就诊人列表
    @Override
    public List<Patient> getPatientList(Long userId) {
        //根据userId 查询就诊人列表
        List<Patient> patientList = baseMapper
                .selectList(new QueryWrapper<Patient>().eq("user_id", userId));
        //远程调用 查询字典内容 封装参数
        patientList.stream().forEach(patient -> {
            this.packPatient(patient);
        });
        return patientList;
    }

    //查询就诊人详情
    @Override
    public Patient getPatientInfo(Long patientId) {
        return this.packPatient(baseMapper.selectById(patientId));
    }

    //添加就诊人
    @Override
    public void savePatient(Patient patient) {
        if (patient != null){
            baseMapper.insert(patient);
        }
    }

    //更新就诊人
    @Override
    public void updatePatientById(Patient patient) {
        if (patient != null){
            baseMapper.updateById(patient);
        }
    }

    //删除就诊人
    @Override
    public void removePatientById(Long id) {
        baseMapper.deleteById(id);
    }

    //封装Patient对象里面的参数
    private Patient packPatient(Patient patient) {
        //证件类型 大陆身份证 港澳台 海外 ...
        String certificatesTypeStr = dictFeignClient.
                getDictionaryNameByDictCodeAndValue(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人证件类型
        String contactsCertificatesTypeStr = dictFeignClient.
                getDictionaryNameByDictCodeAndValue(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //省
        String provinceStr = dictFeignClient.
                getDictionaryNameByDictCodeAndValue(patient.getProvinceCode());
        //市
        String cityStr = dictFeignClient.
                getDictionaryNameByDictCodeAndValue(patient.getCityCode());
        //区
        String districtStr = dictFeignClient.
                getDictionaryNameByDictCodeAndValue(patient.getDistrictCode());
        //地址
        patient.getParam().put("certificatesTypeString", certificatesTypeStr);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeStr);

        patient.getParam().put("provinceString", provinceStr);
        patient.getParam().put("cityString", cityStr);
        patient.getParam().put("districtString", districtStr);

        patient.getParam().put("fullAddress", provinceStr + cityStr + districtStr + patient.getAddress());
        return patient;
    }
}
